﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tetris
{
    class Validation
    {
        public static bool IsOnTheField(int X, int Y, GameField field, List<Figures> listOfFigures)
        {
            bool isOnField = false;
            for (int i = 0; i < listOfFigures.Count; i++)
            {
                if (listOfFigures[i].Xfigure >= field.StartX && listOfFigures[i].Xfigure <= field.StartX + field.Width - 1
                && listOfFigures[i].Yfigure >= field.StartY && listOfFigures[i].Yfigure <= field.StartY + field.Height - 1)
                {
                    isOnField = true;
                }
            }
            
            return isOnField;
        }

        public static bool IsOnFinishedList(List<Figures> golist, List<Figures> finlist)
        {
            bool isOnFinished = false;
            for (int i = 0; i < golist.Count; i++)
            {
                for (int j = 0; j < finlist.Count; j++)
                {
                    if (golist[i].Xfigure == finlist[j].Xfigure && golist[i].Yfigure + 1 == finlist[j].Yfigure)
                    {
                        isOnFinished = true;
                    }
                }
            }
            return isOnFinished;
        }

        public static bool IsOnFinishedListLeftSideMoving(List<Figures> golist, List<Figures> finlist)
        {
            bool isOnFinished = false;
            for (int i = 0; i < golist.Count; i++)
            {
                for (int j = 0; j < finlist.Count; j++)
                {
                    if (golist[i].Xfigure - 1 == finlist[j].Xfigure && golist[i].Yfigure == finlist[j].Yfigure)
                    {
                        isOnFinished = true;
                    }
                }
            }
            return isOnFinished;
        }

        public static bool IsOnFinishedListRightSideMoving(List<Figures> golist, List<Figures> finlist)
        {
            bool isOnFinished = false;
            for (int i = 0; i < golist.Count; i++)
            {
                for (int j = 0; j < finlist.Count; j++)
                {
                    if (golist[i].Xfigure + 1 == finlist[j].Xfigure && golist[i].Yfigure == finlist[j].Yfigure)
                    {
                        isOnFinished = true;
                    }
                }
            }
            return isOnFinished;
        }

        public static bool IsOnDown(GameField field, List<Figures> list)
        {
            bool isOnDown = false;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Yfigure + 1 >= field.StartY + field.Height)
                {
                    isOnDown = true;
                }
            }
            return isOnDown;
        }

        public static bool IsOnRight(GameField field, List<Figures> list)
        {
            bool isOnRight = false;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Xfigure + 1 >= field.StartX + field.Width)
                {
                    isOnRight = true;
                }
            }
            return isOnRight;
        }

        public static bool IsOnLeft(GameField field, List<Figures> list)
        {
            bool isOnLeft = false;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Xfigure - 1 <= field.StartX - 1)
                {
                    isOnLeft = true;
                }
            }
            return isOnLeft;
        }

        public static bool IsLineComplete(List<Figures> list, GameField field)
        {
            bool isCompleteLine = false;
            int duplicates = 0;

            for (int j = field.Height; j >= 1; j--)
            {
                duplicates = 0;
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].Yfigure == j)
                    {
                        duplicates++;
                    }
                }
                if (duplicates == field.Width)
                {
                    isCompleteLine = true;
                }
            }
            return isCompleteLine;

        }

        public static List<int> CoordYCompleteLine(List<Figures> list, GameField field)
        {
            int Yduplicate = field.Height;
            int duplicates = 0;
            List<int> duplictesYcoord = new List<int>();
            for (int j = field.Height; j >= 1; j--)
            {
                duplicates = 0;
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].Yfigure == j)
                    {
                        duplicates++;
                    }
                }
                if (duplicates == field.Width)
                {
                    duplictesYcoord.Add(j);
                }
            }
            return duplictesYcoord;
        }
    }
}
